import { Singleton } from '../Manager/BaseManager';
import { HandleCode, Handler, HandlerBase } from './Handler';
/* eslint-disable */

export class HandlerManager extends Singleton {
  className: string = 'HandlerManager';
  private handles: Map<HandleCode, [(...args: any[]) => void, HandlerBase]> = new Map();
  // private handlers: HandlerBase[] = [];

  initMain() {
    this.registerHandler(Handler.get('MainHandler'));
    // this.registerHandler(Handler.get('ArenaHandler'));
  }

  init() {
    /* mark{0} */
    // this.registerHandler(Handler.get('MainHandler'));
    /* mark{1} */
  }

  registerHandler(handler: HandlerBase) {
    // this.handlers.push(handler);
    handler.handles.forEach(([code, func]: [HandleCode, (...args: any[]) => void]) => {
      this.handles.set(code, [func, handler]);
    });
  }

  handle(code: HandleCode, ...args: any[]) {
    const [func, handler] = this.handles.get(code);
    if (!func) throw new Error('没有注册的请求消息处理函数');
    func.call(handler, ...args);
  }
}
